package com.github.dice.service.impl;

import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Resource;

import com.github.dice.service.IPlayerAndDiceMappingService;
import com.github.dice.service.IRoomAndPlayerMappingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.dice.constant.ErrorEnums;
import com.github.dice.dto.RoomDTO;
import com.github.dice.entity.PlayerAndDiceMapping;
import com.github.dice.entity.Room;
import com.github.dice.entity.RoomOwner;
import com.github.dice.exception.BusinessException;
import com.github.dice.mapper.RoomOwnerMapper;
import com.github.dice.service.IRoomOwnerService;
import com.github.dice.service.IRoomService;

@Service
public class RoomOwnerService extends ServiceImpl<RoomOwnerMapper, RoomOwner> implements IRoomOwnerService {

    @Resource
    private RoomOwnerMapper              roomOwnerMapper;

    @Resource
    private IRoomService                 roomService;

    @Resource
    private IRoomAndPlayerMappingService roomAndPlayerMappingService;

    @Resource
    private IPlayerAndDiceMappingService playerAndDiceMappingService;

    public List<RoomOwner> queryRoomOwnerList(String name) {
        QueryWrapper<RoomOwner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_owner_name", name);
        queryWrapper.eq("is_delete", 0);
        return roomOwnerMapper.selectList(queryWrapper);
    }

    public RoomOwner addRoomOwner(RoomOwner roomOwner) {
        QueryWrapper<RoomOwner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_owner_name", roomOwner.getRoomOwnerName());
        queryWrapper.eq("is_delete", 0);
        RoomOwner selectOne = roomOwnerMapper.selectOne(queryWrapper);
        if (null == selectOne) {
            roomOwner.setIsDelete(0);
            roomOwnerMapper.insert(roomOwner);
            return roomOwner;
        } else {
            return selectOne;
        }
    }

    public RoomOwner getRoomOwnerByRoomOwnerName(@Nonnull String roomOwnerName) {
        QueryWrapper<RoomOwner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_owner_name", roomOwnerName);
        queryWrapper.eq("is_delete", 0);
        return roomOwnerMapper.selectOne(queryWrapper);
    }

    public RoomOwner getRoomOwnerByNameAndPwd(@Nonnull String roomOwnerName, @Nonnull String roomOwnerPwd) {
        QueryWrapper<RoomOwner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_owner_name", roomOwnerName);
        queryWrapper.eq("room_owner_pwd", roomOwnerPwd);
        queryWrapper.eq("is_delete", 0);
        return roomOwnerMapper.selectOne(queryWrapper);
    }

    public Boolean checkAllRoll(String roomNumber) {
        //get room.playerCount
        Room room = roomService.getRoomByNumber(roomNumber);
        //get playerAndDiceMapping
        List<PlayerAndDiceMapping> list = playerAndDiceMappingService.getPlayerAndDiceMappingListByRoom(roomNumber);
        return !CollectionUtils.isEmpty(list) && list.size() == room.getPlayerCount();
    }

    public List<PlayerAndDiceMapping> showResult(String roomNumber, String roomOwnerName) {
        // get roomDTO
        RoomDTO roomDTO = roomService.getRoomDTOByRoomNumber(roomNumber);
        // check is roomOwner
        if (!StringUtils.equals(roomOwnerName, roomDTO.getRoomOwner().getRoomOwnerName())) {
            throw new BusinessException(ErrorEnums.E_6002);
        }
        // get List<PlayerAndDiceMapping>
        return playerAndDiceMappingService.getPlayerAndDiceMappingListByRoom(roomNumber);
    }

    @Override
    public void deleteRoomAndAllMapping(@Nonnull String roomNumber) {
        Room room = roomService.getRoomByNumber(roomNumber);
        if (null != room) {
            roomService.delRoom(roomNumber);
            roomAndPlayerMappingService.delPlayerMappingByRoom(roomNumber);
            playerAndDiceMappingService.delPlayerAndDiceMappingByRoom(roomNumber);
        }
    }
}
